<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
class AccountingAccount
{
	
	const SCOPE = Accountancy;

	static public function getFields ($table = false) {
		$dbManager = new DBManager();
		$fields = $dbManager->MultipleTablesFields("MostrarCamposCuentaContable", self::SCOPE, AccountingAccountsTable, $table);
		return $fields;
	}

	static public function getCamposAlta ($table = false) {
		$dbManager = new DBManager();
		$fields = $dbManager->MultipleTablesFields("MostrarCamposAltaCuentaContable", self::SCOPE, AccountingAccountsTable, $table);
		return $fields;
	}

	static public function Register ($filledFields) {
		$dbManager = new DBManager();
		$result = $dbManager->ExecuteProcedure ('AltaDeCuentaContable',self::SCOPE,$filledFields);		
		if ($result)
			Interfaz::Result($dbManager->GetResult());
		else
			Interfaz::Error($dbManager->GetError());
		return $result;		
	}

	static public function SearchCriterias () {
		$dbManager = new DBManager();
		$criterias = $dbManager->SearchCriterias ('ConsultaDeCuentaContableNinguno',self::SCOPE);
		if (empty($criterias)) {
			Interfaz::Error($dbManager->GetError());
		}
		return $criterias;
	}	

	static public function ConsultarEnIntervalo ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('CuentasEnIntervalo',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function Search ($criterias, $searchType = FALSE) {
		$table = array();
		$dbManager = new DBManager();

		// CUENTAS CONTABLES SIN TERCERO ASOCIADO
		if ($searchType == None || $searchType == All || !$searchType) {
			$tablaNinguno = $dbManager->ExecuteSearchProcedure ('ConsultaDeCuentaContableNinguno',self::SCOPE, $criterias);
			if (!is_array($tablaNinguno))
				Interfaz::Error($dbManager->GetError());
			else			
				$table[None] = $tablaNinguno;
		}
			
		// CUENTAS CONTABLES ASOCIADAS A ORGANIZACIONES
		if ($searchType == Organization || $searchType == All || !$searchType) {
			$tablaOrganizaciones = $dbManager->ExecuteSearchProcedure ('ConsultaDeCuentaContableOrganizaciones',self::SCOPE, $criterias);
			if (!is_array($tablaOrganizaciones))
				Interfaz::Error($dbManager->GetError());
			else			
				$table[Organization] = $tablaOrganizaciones;
		}

		// CUENTAS CONTABLES ASOCIADAS A PERSONAS
		if ($searchType == Person || $searchType == All || !$searchType) {
			$peopleTable = $dbManager->ExecuteSearchProcedure ('ConsultaDeCuentaContablePersonas',self::SCOPE, $criterias);
			if (!is_array($peopleTable))
				Interfaz::Error($dbManager->GetError());
			else			
				$table[Person] = $peopleTable;
		}
		
		return $table;
	}

	static public function SeeDetails ($accountType,$identifier) {
		$dbManager = new DBManager();

		switch ($accountType) {
			// CUENTAS CONTABLES SIN TERCERO ASOCIADO		
  			case None:
					$table = $dbManager->ExecuteSearchProcedure ('DetalleDeCuentaContableNinguno',self::SCOPE, $identifier);
					break;
		
			// CUENTAS CONTABLES ASOCIADAS A ORGANIZACIONES
			case Organization:
					$table = $dbManager->ExecuteSearchProcedure ('DetalleDeCuentaContableOrganizaciones',self::SCOPE, $identifier);
					break;
	
			// CUENTAS CONTABLES ASOCIADAS A PERSONAS
			case Person:
					$table = $dbManager->ExecuteSearchProcedure ('DetalleDeCuentaContablePersonas',self::SCOPE, $identifier);
					break;
		}

		if (!is_array($table) || empty($table)) {
			Interfaz::Error($dbManager->GetError());
			return FALSE;
		}
		return $table[0];
	}
	
	static public function Modify ($filledFields) {
		$dbManager = new DBManager();
		$result = $dbManager->ExecuteProcedure ('ModificacionDeCuentaContable',self::SCOPE,$filledFields);		
		if ($result)
			Interfaz::Result($dbManager->GetResult());
		else
			Interfaz::Error($dbManager->GetError());
		return $result;		

	}

	static public function Remove ($filledFields) {
		$dbManager = new DBManager();
		$result = $dbManager->ExecuteProcedure ('BajaDeCuentaContable',self::SCOPE,$filledFields);		
		if ($result)
			Interfaz::Result($dbManager->GetResult());
		else
			Interfaz::Error($dbManager->GetError());
		return $result;		

	}

	static public function CriteriosBalanceMayor () {
		$dbManager = new DBManager();
		$criterias = $dbManager->SearchCriterias ('MayorDeCuenta',self::SCOPE);
		if (empty($criterias)) {
			Interfaz::Error($dbManager->GetError());
		}
		return $criterias;
	}

	static public function BalanceMayor ($filledFields) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('MayorDeCuenta',self::SCOPE,$filledFields);		
		if (!is_array($table)) {
			Interfaz::Error($dbManager->GetError());
			return FALSE;
		}
		return $table;
	}

	static public function TotalBalanceMayor ($filledFields) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('MayorDeCuentaTotal',self::SCOPE,$filledFields);		
		if (!is_array($table)) {
			Interfaz::Error($dbManager->GetError());
			return FALSE;
		}
		return $table;
	}

	static public function CriteriosBalanceSumasSaldos () {
		$StartDate = Interfaz::CreateField("StartDate", Date, Yes);
		$EndDate = Interfaz::CreateField("EndDate", Date, Yes);
		$Clases = Interfaz::CreateField("Clases",CheckBox,Yes);
		$Cuentas = Interfaz::CreateField("Cuentas",CheckBox,Yes);
		$Subcuentas = Interfaz::CreateField("Subcuentas",CheckBox,Yes);
		
		$fields = Interfaz::AddFields($Clases, $Cuentas);
		$fields = Interfaz::AddFields($fields, $Subcuentas);
		$fields = Interfaz::AddFields($fields, $StartDate);
		$fields = Interfaz::AddFields($fields, $EndDate);
		return $fields;

	}

	static public function ObtenerClasesSumasSaldos () {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ObtenerClases',self::SCOPE, array());
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerCuentasSumasSaldos ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ObtenerCuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerSubcuentasSumasSaldos ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ObtenerSubcuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function SumasSaldosCuenta ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('SumasSaldosCuenta',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function SumasSaldosClase ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('SumasSaldosClase',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function SumasSaldosSubcuenta ($criterias) {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('SumasSaldosSubcuenta',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function CriteriosBalanceActivoPasivo () {
		$StartDate = Interfaz::CreateField("StartDate", Date, Yes);
		$EndDate = Interfaz::CreateField("EndDate", Date, Yes);
		$Cuentas = Interfaz::CreateField("Cuentas",CheckBox,Yes);
		$Subcuentas = Interfaz::CreateField("Subcuentas",CheckBox,Yes);
		
		$fields = Interfaz::AddFields($Cuentas, $Subcuentas);
		$fields = Interfaz::AddFields($fields, $StartDate);
		$fields = Interfaz::AddFields($fields, $EndDate);
		return $fields;

	}

	static public function ObtenerClasesActivo () {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ClasesInformes',self::SCOPE, array(accountType => Asset));
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerCuentasActivo ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Asset));
		$table = $dbManager->ExecuteSearchProcedure ('CuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerSubcuentasActivo ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Asset));
		$table = $dbManager->ExecuteSearchProcedure ('SubcuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerClasesPasivo () {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ClasesInformes',self::SCOPE, array(accountType => Pasivo));
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerCuentasPasivo ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Pasivo));
		$table = $dbManager->ExecuteSearchProcedure ('CuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerSubcuentasPasivo ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Pasivo));
		$table = $dbManager->ExecuteSearchProcedure ('SubcuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ActivoClases ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Asset));
		$table = $dbManager->ExecuteSearchProcedure ('InformesClases',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ActivoCuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Asset));
		$table = $dbManager->ExecuteSearchProcedure ('InformesCuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ActivoSubcuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Asset));
		$table = $dbManager->ExecuteSearchProcedure ('InformesSubcuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function PasivoClases ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Pasivo));
		$table = $dbManager->ExecuteSearchProcedure ('InformesClases',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function PasivoCuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Pasivo));
		$table = $dbManager->ExecuteSearchProcedure ('InformesCuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function PasivoSubcuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Pasivo));
		$table = $dbManager->ExecuteSearchProcedure ('InformesSubcuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}
	
	static public function ObtenerClasesIngresos () {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ClasesInformes',self::SCOPE, array(accountType => Income));
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerCuentasIngresos ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Income));
		$table = $dbManager->ExecuteSearchProcedure ('CuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function IngresosClases ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Income));
		$table = $dbManager->ExecuteSearchProcedure ('InformesClases',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function IngresosCuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Income));
		$table = $dbManager->ExecuteSearchProcedure ('InformesCuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerClasesGastos () {
		$dbManager = new DBManager();
		$table = $dbManager->ExecuteSearchProcedure ('ClasesInformes',self::SCOPE, array(accountType => Expenses));
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerCuentasGastos ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Expenses));
		$table = $dbManager->ExecuteSearchProcedure ('CuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}
	
	static public function GastosClases ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Expenses));
		$table = $dbManager->ExecuteSearchProcedure ('InformesClases',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function GastosCuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Expenses));
		$table = $dbManager->ExecuteSearchProcedure ('InformesCuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerSubcuentasIngresos ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Income));
		$table = $dbManager->ExecuteSearchProcedure ('SubcuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function ObtenerSubcuentasGastos ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Expenses));
		$table = $dbManager->ExecuteSearchProcedure ('SubcuentasInformes',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function IngresosSubcuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Income));
		$table = $dbManager->ExecuteSearchProcedure ('InformesSubcuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

	static public function GastosSubcuentas ($criterias) {
		$dbManager = new DBManager();
		$criterias = array_merge ($criterias, array(accountType => Expenses));
		$table = $dbManager->ExecuteSearchProcedure ('InformesSubcuentas',self::SCOPE, $criterias);
		if (!is_array($table))
			return FALSE;
		return $table;
	}

}